package nus.iss.laps;

import java.util.List;
import java.util.Locale;

import nus.iss.laps.cri.Criteria;
import nus.iss.laps.dao.HolidayDAO;
import nus.iss.laps.general.LapsUtil;
import nus.iss.laps.model.Holiday;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

@Controller
public class HolidayController {
	
	@Autowired
	HolidayDAO holidayDAO;
	
	@RequestMapping(value = "/holidays", method = RequestMethod.GET)
	public String holidayList(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setRowIndex(0);
			l_cri.setRowLimit(5);
			
			List<Holiday> l_listHoliday = holidayDAO.getHolidays(l_cri);
			model.addAttribute("holidayList", l_listHoliday);
			model.addAttribute("criteria", l_cri);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "list_holiday";
	}
	
	@RequestMapping(value = "/holidays", method = RequestMethod.POST)
	public String rowLimitChange(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setRowIndex(0);
			l_cri.setKeyword(webRequest.getParameter("keyword"));
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			
			List<Holiday> l_listHoliday = holidayDAO.getHolidays(l_cri);
			model.addAttribute("holidayList", l_listHoliday);
			model.addAttribute("criteria", l_cri);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "list_holiday";
	}
	
	@RequestMapping(value = "/holidays", method = RequestMethod.POST, params = "btnNext")
	public String next(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setKeyword(webRequest.getParameter("keyword"));
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			l_cri.setRowIndex(Integer.parseInt(webRequest.getParameter("rowIndex")) + l_cri.getRowLimit());
			List<Holiday> l_listHoliday = holidayDAO.getHolidays(l_cri);
			model.addAttribute("criteria", l_cri);
			model.addAttribute("holidayList", l_listHoliday);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "list_holiday";
	}
	
	@RequestMapping(value = "/holidays", method = RequestMethod.POST, params = "btnPrev")
	public String previous(Model model, WebRequest webRequest)
	{
		try {
			Criteria l_cri = new Criteria();
			l_cri.setKeyword(webRequest.getParameter("keyword"));
			l_cri.setRowLimit(Integer.parseInt(webRequest.getParameter("cboRows")));
			l_cri.setRowIndex(Integer.parseInt(webRequest.getParameter("rowIndex")) - l_cri.getRowLimit());
			List<Holiday> l_listHoliday = holidayDAO.getHolidays(l_cri);
			model.addAttribute("criteria", l_cri);
			model.addAttribute("holidayList", l_listHoliday);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "list_holiday";
	}
	
	@RequestMapping(value = "/holidays/new", method = RequestMethod.GET)
	public String newHoliday(Locale locale, Model model)
	{
		model.addAttribute("holiday", new Holiday());
		return "holiday";
	}
	
	@RequestMapping(value = "/holidays/*", method = RequestMethod.POST, params = "save")
	public String saveHoliday(Model model, @ModelAttribute("SpringWeb")Holiday holiday)
	{
		try {
			holiday.setHoliday(LapsUtil.formattedDateToString(holiday.getHoliday()));
			
			if(holidayDAO.isExisted(holiday.getHoliday()))
			{
				return "error";
			}
			else
				holidayDAO.addHoliday(holiday);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "redirect:/holidays";
	}
	
	@RequestMapping(value = "/holidays/*", method = RequestMethod.POST, params = "edit")
	public String editHoliday(Model model, @ModelAttribute("SpringWeb")Holiday holiday)
	{
		try {
			holiday.setHoliday(LapsUtil.formattedDateToString(holiday.getHoliday()));
			holidayDAO.updateHoliday(holiday);
			return "redirect:/holidays";
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
	}
	
	@RequestMapping(value = "/holidays/*", method = RequestMethod.POST, params = "delete")
	public String deleteHoliday(Model model, WebRequest webRequest)
	{
		try {
			String l_date = LapsUtil.formattedDateToString(webRequest.getParameter("holiday"));
			holidayDAO.deleteHoliday(l_date);
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "redirect:/holidays";
	}
	
	@RequestMapping(value = "/holidays/{holiday}", method = RequestMethod.GET)
	public String viewHoliday(Locale locale, Model model, @PathVariable String holiday)
	{
		try {
			Holiday l_holiday = holidayDAO.getHoliday(holiday);
			model.addAttribute("holiday", l_holiday!= null ? l_holiday : new Holiday());
		} catch (Exception ex) {
			model.addAttribute("error", ex.getMessage());
			return "error";
		}
		return "holiday";
	}
}
